
/**
 * 测试 gc 引用计数法
 * @author dormi330
 */

public class ReferenceCountGC {
	private static final int _1MB = 1024 * 1024;
	private byte[] bigSize = new byte[2 * _1MB];

	private Object instance = null;

	public static void main(String[] args) {
		// 造成一个循环引用
		ReferenceCountGC objA = new ReferenceCountGC();		
		ReferenceCountGC objB = new ReferenceCountGC();		

		objA.instance = objB;
		objB.instance = objA;

		// 设置 null, 看GC 是否回收这2个object
		objA = null;
		objB = null;

		System.gc();
/*
output:
java -verbosegc ReferenceCountGC
[GC (System.gc())  5427K->392K(125952K), 0.0009488 secs]
[Full GC (System.gc())  392K->276K(125952K), 0.0036867 secs]

循环引用, 也进行了gc 5427K->392K
*/
	}
}